﻿package CutLoad.mc
{
	import flash.display.Graphics;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;

	/**
	 * 拖动组装器基础类，简单的包含了拖动的代码
	 * <br>使用这个类需要：提供必要的皮肤，拖动范围就会成为一个拖动条，注：1.各元件的注册点必须位于左上角,2. 拖动范围的起始点x或y要为一个整数，如果有小数点，可能会有一点儿偏差。
	 * @author smallerbird
	 * 
	 */	
	public class DragBarBase
	{
		private var scrollArea:Rectangle,scrollBar:Sprite
		private var isstopdrag:Boolean=true;
		//
		private var funValue:Function,funStopDrag:Function;
		//
		private var startX:Number=0;
		//
		private var lastValue:Number;
		//drag
		private var dragX1:Number,dragX2:Number,dragY1:Number,dragY2:Number;
		/**
		 * 
		 * @param scrollArea 拖动范围
		 * @param scrollBar 拖动条
		 * @param funValue 拖动时调用的方法     格式:function(拖动条当前值)
		 * @param funStopDrag 停止拖动时调用的方法    格式:function(拖动条当前值)
		 * 
		 */		
		public function DragBarBase(scrollArea:Rectangle,scrollBar:Sprite,funValue:Function=null,funStopDrag:Function=null)
		{
			this.scrollArea=scrollArea;
			this.scrollBar=scrollBar;
			this.funValue=funValue;
			this.funStopDrag=funStopDrag;
			//
			scrollBar.x=scrollArea.x;
			scrollBar.y=scrollArea.y;
			//
			startX=scrollBar.x;
			//
			dragX1=scrollArea.x-scrollBar.width;
			dragX2=(dragX1+scrollArea.width)+scrollBar.width*3
			dragY1=	scrollBar.y
			dragY2=scrollBar.y+scrollBar.height;
			//
			scrollBar.addEventListener(MouseEvent.MOUSE_DOWN,funDown);
			scrollBar.addEventListener(MouseEvent.MOUSE_UP,funUp);
			scrollBar.addEventListener(Event.ENTER_FRAME,funEnter);
			if(scrollBar.stage!=null) scrollBar.stage.addEventListener(MouseEvent.MOUSE_UP,mouse_up);
		}
		private function mouse_up(e:MouseEvent):void{
			stopBarDrag();
		}
		/**
		 *是否在拖动 
		 * @return 
		 * 
		 */		
		public function getIsStartDrag():Boolean{
			return !isstopdrag;
		}
		private function funDown(e:MouseEvent):void{
			scrollBar.startDrag(false,scrollArea);
			isstopdrag=false;
		}
		private function funUp(e:MouseEvent):void{
			stopBarDrag();
			if(funStopDrag!=null) funStopDrag(this.value);
		}
		private function funEnter(e:Event):void{
			/*if(!isstopdrag){
				var xx:Number=scrollBar.stage.mouseX;
				var yy:Number=scrollBar.stage.mouseY;
				if(xx<dragX1||xx>dragX2||yy<dragY1||yy>dragY2){
					stopBarDrag()
				}
			}*/
			if(funValue!=null){
				if(lastValue!=value){
					lastValue=value;
					funValue(value)
				}
			}
		}
		public function get value():Number{
			var numP:Number=Math.round(((scrollBar.x-scrollArea.x)/scrollArea.width)*100)/100
			if(numP<0.01) numP=0
			if(numP>0.99) numP=1;
			return numP;
		}
		public function set value(numP:Number):void{
			scrollBar.x=startX+scrollArea.width*numP;
			if(funValue!=null) funValue(numP)
		}
		public function left(speed:Number):void{
			speed=Math.abs(speed);
			var xx:Number=scrollBar.x+speed;
			if(xx>startX+scrollArea.width) xx=startX+scrollArea.width
			scrollBar.x=xx;
		}
		public function right(speed:Number):void{
			speed=Math.abs(speed);
			var xx:Number=scrollBar.x-speed;
			if(xx<startX) xx=startX;
			scrollBar.x=xx;
		}
		private function stopBarDrag():void{
			scrollBar.stopDrag();
			isstopdrag=true;
		}
		public function killMe():void{
			if(scrollBar.hasEventListener(MouseEvent.MOUSE_DOWN)) scrollBar.addEventListener(MouseEvent.MOUSE_DOWN,funDown);
			if(scrollBar.hasEventListener(MouseEvent.MOUSE_UP)) scrollBar.addEventListener(MouseEvent.MOUSE_UP,funUp);
			//scrollBar.addEventListener(Event.ENTER_FRAME,funEnter);
			if(scrollBar.stage!=null){
				if(scrollBar.stage.hasEventListener(MouseEvent.MOUSE_UP))scrollBar.stage.addEventListener(MouseEvent.MOUSE_UP,mouse_up);
				
			}
		}
	}
}